package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SupplierService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;
    @Autowired
    private LogService logService;

    /**
     * 分页查询供应商信息
     *
     * @param page 当前页数
     * @param rows 每页显示的记录数
     * @return
     */
    @PostMapping("/list")
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
        return supplierService.list(page, rows, supplierName);
    }

    @PostMapping("/save")//http://localhost:8080/supplier/save?supplierId=1;两种方式获取1、request 2.supplier.getId
    public ServiceVO saveOrUpdate(Supplier supplier,
                                  HttpServletRequest request) {
        String supplierId = request.getParameter("supplierId");
        return supplierService.saveOrUpdate(supplier, supplierId);
    }

    @PostMapping("/delete")
    public ServiceVO delete(String ids)throws Exception{
        String []idsStr=ids.split(",");
        for(int i=0;i<idsStr.length;i++) {
            int id = Integer.parseInt(idsStr[i]);
            supplierService.delete(id);
        }
       return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @PostMapping("/getComboboxList")
    public List<Supplier> getComboboxList(String q){//供应商名称模糊查询
        return supplierService.getComboboxList(q);
    }
}
